package com.newlink.building.userinfo.mvp

import com.newlink.building.common_base.base.BasePresenter
import com.newlink.building.common_base.ext.ss
import com.newlink.building.common_base.model.bean.MessageCountRequest
import com.newlink.building.common_base.utils.logDebug
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_PersonalContract
import com.newlink.building.userinfo.mvp.model.Module_UserInfo_PersonalModel

/**
 * @author wangyang
 * @date 2022/9/16
 * @description 我的页面的Presenter
 */
class Module_UserInfo_PersonalPresenter : BasePresenter<Module_UserInfo_PersonalContract.Model, Module_UserInfo_PersonalContract.View>(),
    Module_UserInfo_PersonalContract.Presenter {

    override fun createModel(): Module_UserInfo_PersonalContract.Model {
        return Module_UserInfo_PersonalModel()
    }

    override fun getUnreadFlag(token: String, type: Int) {
        mModel?.getUnreadFlag(token, type)?.ss(mModel, mView) {
            if (it?.code == 200 && it?.data != null) {
                val msgNum = it.data.msgNum ?: 0
                val noticeNum = it.data.noticeNum ?: 0
                val unread = it?.data!!.msgNum + it?.data!!.noticeNum
                mView?.getUnreadFlagSuccess(unread > 0, msgNum, noticeNum)
            } else {
                mView?.onFail()
            }
        }
    }

    override fun getNoticeUnreadCount(token: String, type: Int) {
        mModel?.getNoticeUnreadCount(token, type)?.ss(mModel, mView) {
            if (it?.code == 200) {
                val unread = it?.data!! > 0
                mView?.getNoticeUnreadFlagSuccess(unread)
            } else {
                mView?.onFail()
            }
        }
    }

    override fun getLocalUnread(token: String, userId: Long) {
        val request = MessageCountRequest(accessToken = token, uId = "$userId")
        mModel?.getLocalUnread(request)
    }

    override fun getUserInfo(token: String, userId: Long) {
        mModel?.getUserInfo(token, userId)?.ss(mModel, mView) { infoData ->
            logDebug<Module_UserInfo_PersonalPresenter>("[jake] getUserInformation response ->  ${infoData?.data}")
            if (infoData?.data == null) {
                mView?.onFail()
            } else {
                mView?.getUserInfoSuccess(infoData?.data!!)
            }
        }
    }

    override fun logout(token: String) {
        mModel?.logout(token)?.ss(mModel, mView) { logoutData ->
           logDebug<Module_UserInfo_PersonalPresenter>("logout response ->  ${logoutData?.data}")
            if (logoutData?.code == 200) {
                mView?.logoutSuccess()
            } else {
                mView?.onFail()
            }
        }
    }

    override fun getAgreement() {
        mModel?.getAgreement()?.ss(mModel, mView) {
            mView?.onGetAgreementSuccess(it?.data)
        }
    }

    override fun getFAQ() {
        mModel?.getFAQ()?.ss(mModel, mView) {
            mView?.onGetFAQSuccess(it?.data)
        }
    }
}